<?php session_start();
require('../globals/setup.php');
$smarty = new Smarty_Kalender;	

$host  = $_SERVER['HTTP_HOST'];
if ($_SERVER['SERVER_NAME'] == 'localhost'){$uri= URI_L;}else{$uri= URI_P;}
if (!isset($_SESSION['id_user']) || $_SESSION['id_user']=='') {
	echo "<script language=Javascript> location.href='http://$host$uri/index.php'; </script>";	
}else{
	
	if (!is_object($bd)){	
		//me conecto a la base de datos
		require("../globals/Db.class.php");
		require("../globals/Conf.class.php");
		$bd=Db::getInstance();	
	}
	if (!is_object($util)){	
		include("../globals/Util.class.php");		
		$util=Util::getInstance();
	}
	

	if (!($util->administrarTiempoSesion())){
		echo "<script language=Javascript> location.href='http://$host$uri/timeout.php'; </script>";
	}else if (!($util->esCliente())){
		echo "<script language=Javascript> location.href='http://$host$uri/sinAcceso.php'; </script>";
	}
	
	$id_user = $_SESSION['id_user'];
	
		
	if (isset($_GET['qry'])){
		$qry = $_GET['qry'];
		$params = explode('_',$qry);		
		$smarty->assign("qry", $qry);
		
		$query_arch = "select id, nombre, numero_cert, fecha_subida, path from archivos where ";
		$or = "";
		for ($j=0; $j<sizeof($params); $j++){
			$valor = $params[$j];
			$query_arch .= " $or (upper(nombre) like upper('%$valor%') and usuario_id = $id_user)";
			$query_arch .= " or (upper(numero_cert) like upper('%$valor%') and usuario_id = $id_user) ";	
			$query_arch .= " or (upper(laboratorio) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(certificadora) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(observaciones) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(marca) like upper('%$valor%') and usuario_id = $id_user) ";	
			$query_arch .= " or (upper(modelo) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(origen) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(producto) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(cantidad) like upper('%$valor%') and usuario_id = $id_user) ";
			$query_arch .= " or (upper(caract_desc) like upper('%$valor%') and usuario_id = $id_user) ";
			$or = "or";
		}		
		$query_arch .= " order by id desc limit 0,999999";
		$resultado_arch = $bd->eje($query_arch);		
		$num_rows_arch = $bd->numRows($resultado_arch);
	}else{		
		$num_rows_arch = 0;		
	}
	
	$mensaje_vacio = "No se han encontrado documentos.";
	$smarty->assign("mensaje_vacio", $mensaje_vacio);
	
	$smarty->assign("num_rows_arch", $num_rows_arch);
	$smarty->assign("hay_archivos", 'false');
	if ($num_rows_arch > 0){
		$smarty->assign("hay_archivos", 'true');
	}
	
	$smarty->assign("array_resultado", $util->getArchivosResultadoArray($bd, $resultado_arch, $num_rows_arch));
	
	$menuItem = "consultas";
	$smarty->assign("menuItem", $menuItem);
	include("../globals/menuIzquierdaClientes.php");
	
	$smarty->display("clientes/consultas.tpl");




} //del if grande ?>